package com.atguigu.distributed.lock.leecode.array;

public class Arrys2 {
    public static void main(String[] args) {
        int[][] arr = new int[3][4];
        Arrys2 arrys2 = new Arrys2();
        int[][] ints = arrys2.generateMatrix2(5, 6);
        for (int i = 0; i < ints.length; i++) {
            int[] anInt = ints[i];
            for (int j = 0; j < anInt.length; j++) {
                System.out.print(anInt[j] + " ");
            }
            System.out.println();


        }
        System.out.println("res==>" + arr.length);
    }

    public int[][] generateMatrix(int m, int n) {
        int num = 1;
        int[][] matrix = new int[m][n];
        int left = 0, right = n - 1, top = 0, bottom = m - 1;
        while (left <= right && top <= bottom) {
            for (int column = left; column <= right; column++) {
                matrix[top][column] = num;
                num++;
            }
            for (int row = top + 1; row <= bottom; row++) {
                matrix[row][right] = num;
                num++;
            }
            if (left < right && top < bottom) {
                for (int column = right - 1; column > left; column--) {
                    matrix[bottom][column] = num;
                    num++;
                }
                for (int row = bottom; row > top; row--) {
                    matrix[row][left] = num;
                    num++;
                }
            }
            left++;
            right--;
            top++;
            bottom--;
        }
        return matrix;
    }

    public int[][] generateMatrix2(int m, int n) {
        int num = 1;
        int[][] matrix = new int[m][n];
        int left = 0, right = n - 1, top = 0, bottom = m - 1;
        while (left <= right && top <= bottom) {
            for (int column = left; column <= right; column++) {
                matrix[top][column] = num;
                num++;
            }
            for (int row = top + 1; row <= bottom; row++) {
                matrix[row][right] = num;
                num++;
            }
            if (left < right && top < bottom) {
                for (int column = right - 1; column > left; column--) {
                    matrix[bottom][column] = num;
                    num++;
                }
                for (int row = bottom; row > top; row--) {
                    matrix[row][left] = num;
                    num++;
                }
            }
            left++;
            right--;
            top++;
            bottom--;
        }
        return matrix;

    }


}
